<template>
	<view>
		<view class="item">
			<view class="top">
				<view class="avatar"><img :src="detail.user.avatar"></view>
			</view>
			<view class="right">
				<view class="name">{{detail.user.name}}</view>
				<view class="content">{{detail.pyqContent}}</view>
				<view class="picture"><img :src="detail.pyqPicture" @click="previewImage"></view>
				<view class="time">{{detail.gmtCreate}}</view>
			</view>
		</view>
		<view class="comment" v-if="comments.length!=0">
			<view class="icon"><img src="@/static/icons/comment.png"></view>
			<view class="content">
				<view class="cItem" v-for="item in comments" :key="item.commentId" @click="reply(item.userSend)">
					<view class="cAvatar"><img :src="item.userSend.avatar"></view>
					<view class="right">
						<span class="cName">{{item.userSend.name}}</span>
						<span class="cTime">{{item.gmtCreate}}</span>
						<span class="cComment">
							<span v-if="item.reviewTo">
								回复<span class="replay">{{item.reviewTo.name}}</span>:
							</span>
							{{item.comment}}
						</span>
					</view>
				</view>
			</view>
		</view>
		<view class="standView"></view>
		<view class="footer">
			<u--textarea v-model="comment" :placeholder="placeholder" autoHeight maxlength="-1" :focus="isFocus"
				@blur="reset" confirmType="done"></u--textarea>
			<u-button text="发送" @click="post"></u-button>
		</view>
	</view>
</template>

<script>
	import {
		nextTick
	} from "vue";
	export default {
		data() {
			return {
				isFocus: false,
				isBlur: true,
				detail: {},
				comments: [
					// {
					// 	userSend: {
					// 		avatar: 'https://gulicollege-picture.oss-cn-guangzhou.aliyuncs.com/2023/02/18/601b7aba0fcb448ebf40de82eb844e2bQXWI3UCLRJ954ac30601979a433c804f3ccf18bfb6c5.jpg',
					// 		name: '三角鸡',
					// 	},
					// 	gmtCreate: '2023年2月18日 14:16',
					// 	comment: '毕设快快结束吧天！毕设快快结束吧天！毕设快快结束吧天！毕设快快结束吧天！毕设快快结束吧天！'
					// },
					// {
					// 	userSend: {
					// 		avatar: 'https://gulicollege-picture.oss-cn-guangzhou.aliyuncs.com/2023/02/18/601b7aba0fcb448ebf40de82eb844e2bQXWI3UCLRJ954ac30601979a433c804f3ccf18bfb6c5.jpg',
					// 		name: '三角鸡',
					// 	},
					// 	gmtCreate: '2023年2月18日 14:16',
					// 	comment: '毕设快快结束吧天！毕设快快结束吧天！毕设快快结束吧天！毕设快快结束吧天！毕设快快结束吧天！'
					// }
				],
				comment: '',
				placeholder: '评论',
				userSend: {}

			}
		},
		onLoad(options) {
			this.detail = JSON.parse(decodeURIComponent(options.info))
		},
		onShow() {
			this.getComments()
		},
		methods: {
			// 判断是否已登录
			async checkLogin() {
				if (uni.getStorageSync("token")) {
					// 获取用户信息
					const {
						data: res
					} = await this.$myRequest({
						url: '/loginBefore/getMemberInfo',
						header: {
							'content-type': 'application/json;charset:utf-8',
							'token': uni.getStorageSync("token"),
						}
					})
					console.log(res);
					if (res.code == 20000) {
						// 将用户id放进缓存
						uni.setStorageSync('userId', res.data.userInfo.userId)
					} else if (res.code == 20001) {
						uni.showToast({
							title: '登录已过期,请重新登录!',
							icon: 'none'
						})
						uni.clearStorageSync()

					}
				} else {
					uni.showToast({
						title: '请先登录!',
						icon: 'error'
					});
				}
			},
			//预览动态图片大图
			previewImage() {
				var that = this;
				uni.previewImage({
					urls: [this.detail.pyqPicture],
				});
			},
			// 获取评论
			async getComments() {
				let {
					data: res
				} = await this.$myRequest({
					url: '/PyqBefore/getPyqComment/' + this.detail.pyqId,
				})
				if (res.code == 20000) {
					this.comments = res.data.list
					console.log(res.data);
				}
			},
			// placeholder文字修改成回复某某
			reply(userSend) {
				this.isFocus = true //获取焦点
				this.placeholder = '回复' + userSend.name + '：';
				this.comment = ''
				this.userSend = userSend
				console.log(this.userSend);
			},
			// textarea的placeholder重置为评论
			reset(name) {
				this.userSend = ''
				this.placeholder = '评论'
				this.isFocus = false
			},
			// 发表评论
			async post() {
				var that = this
				if (!uni.getStorageSync('userId')) {
					this.checkLogin()
				} else {
					let {
						data: res
					} = await this.$myRequest({
						url: '/PyqBefore/addPyqComment',
						method: 'post',
						data: {
							reviewTo: that.userSend.userId || '',
							comment: that.comment,
							pyqId: that.detail.pyqId,
							userId: uni.getStorageSync('userId')
						}
					})
					console.log(this.detail);
					if (res.code == 20000) {
						this.getComments()
						this.comment = ''
					}
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.main {
		height: 90%;
	}

	.item {
		padding: 30rpx 30rpx 0;
		display: flex;

		.avatar {
			margin-right: 20rpx;

			img {
				width: 80rpx;
				height: 80rpx;
				border-radius: 10rpx;
			}
		}

		.name {
			color: #d81e06;
			font-weight: bold;
			margin-bottom: 10rpx;
		}

		.picture {
			margin-top: 10rpx;

			img {
				width: 160rpx;
				height: 160rpx;
			}
		}

		.time {
			color: #8e8e8e;
			font-size: 24rpx;
		}
	}

	.comment {
		background-color: #F7F7F7;
		border-radius: 20rpx;
		margin: 10rpx;
		border: 1px solid transparent;
		display: flex;

		.icon {
			width: 30rpx;
			height: 30rpx;
			margin-top: 35rpx;
			margin-left: 20rpx;

			img {
				width: 30rpx;
				height: 30rpx;
			}
		}

		.content {
			width: 90%;
		}

		.cItem {
			background-color: #F7F7F7;
			float: right;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #9d9d9d;
			position: relative;
			display: flex;
			width: 98%;

			.cAvatar {
				width: 60rpx;
				height: 60rpx;
				display: inline-block;
				margin-right: 30rpx;

				img {
					width: 70rpx;
					height: 70rpx;
					border-radius: 10rpx;
				}
			}

			.right {
				.cName {
					color: #d81e06;
					font-weight: bold;
				}

				.cTime {
					color: #8e8e8e;
					font-size: 24rpx;
					position: absolute;
					right: 0;
				}

				.cComment {
					display: block;
					margin-top: 5rpx;

					.replay {
						color: #d81e06;
						font-weight: bold;
					}
				}
			}

		}
	}

	.standView {
		width: 100%;
		height: 200rpx;
	}

	.footer {

		width: 100%;
		position: fixed;
		bottom: 0;
		display: flex;
		background-color: #f5f5f5;
		align-items: flex-end;

		/deep/ .u--textarea {
			margin: 20rpx;
		}

		/deep/.u-button {
			width: 15%;
			height: 76rpx;
		}
	}
</style>
